﻿
using Sys;

using System.Silverlight;
using System.Silverlight.Presentation;
using System.Silverlight.Presentation.Animation;
using System;
using seh = Sys.EventHandler;
using sea = Sys.EventArgs;

namespace SBT
{
    public abstract class PageFlipScrollerBase : IScroller
    {
        private string m_animDuration = "00:00:00.3000000";

        public string AnimDuration
        {
            get { return m_animDuration; }
            set { m_animDuration = value; }
        }

        private double m_keySplineX0 = 0;
        public double KeySplineX0
        {
            get { return m_keySplineX0; }
            set { m_keySplineX0 = value; }
        }
        private double m_keySplineY0 = 0;
        public double KeySplineY0
        {
            get { return m_keySplineY0; }
            set { m_keySplineY0 = value; }
        }
        private double m_keySplineX1 = 1;
        public double KeySplineX1
        {
            get { return m_keySplineX1; }
            set { m_keySplineX1 = value; }
        }
        private double m_keySplineY1 = 1;
        public double KeySplineY1
        {
            get { return m_keySplineY1; }
            set { m_keySplineY1 = value; }
        }
        public void SetKeySpline(double x0, double y0, double x1, double y1)
        {
            m_keySplineX0 = x0;
            m_keySplineX1 = x1;
            m_keySplineY0 = y0;
            m_keySplineY1 = y1;
        }
        private double m_stepSize;
        public double StepSize
        {
            get { return m_stepSize; }
            set { m_stepSize = value; }
        }
        
        protected abstract string TranslateAxis { get; }
        private Storyboard m_pageFlipperAnim;
        
        private SplineDoubleKeyFrame m_doubleKey;
        protected SplineDoubleKeyFrame DoubleKey
        {
            get { return m_doubleKey; }
        }

        private bool m_animRunning = false;
        protected bool AnimRunning { get { return m_animRunning; } }
        private bool m_initialized = false;
        protected void InitAnim()
        {
            if (m_initialized)
                return;

            m_initialized = true;
            /*
             <Storyboard x:Name="sampleAnim">
            <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="rectangle" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
                <SplineDoubleKeyFrame KeyTime="00:00:00.4000000" Value="151"/>
            </DoubleAnimationUsingKeyFrames>
        </Storyboard>
             * */
            string storyBoardName = AgBehavior.GenerateName(m_scrollContainer.Plugin);
            string keyFrameName = AgBehavior.GenerateName(m_scrollContainer.Plugin);
            string storyBoardXaml = "<Storyboard xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" x:Name=\"" + storyBoardName + "\"><DoubleAnimationUsingKeyFrames BeginTime=\"00:00:00\" Storyboard.TargetName=\"" + m_scrollContainer.ContentBoxName + "\" Storyboard.TargetProperty=\"(UIElement.RenderTransform).(TranslateTransform."+TranslateAxis+")\"><SplineDoubleKeyFrame x:Name=\"" + keyFrameName + "\" KeyTime=\""+this.m_animDuration+"\" Value=\"0\" KeySpline=\""+m_keySplineX0+","+m_keySplineY0+" "+m_keySplineX1+","+m_keySplineY1+"\"/></DoubleAnimationUsingKeyFrames></Storyboard>";

            DependencyObject obj = m_scrollContainer.Plugin.Content.CreateFromXaml(storyBoardXaml);
            m_scrollContainer.Plugin.Content.Root.Resources.Add(obj);

            m_pageFlipperAnim = (Storyboard)obj;
            m_doubleKey = (SplineDoubleKeyFrame)(object)m_scrollContainer.Plugin.Content.FindName(keyFrameName);
            m_pageFlipperAnim.AddEventListener(TimelineEvent.Completed, new seh(OnAnimCompleted));

        }
        private void OnAnimCompleted(object sender, sea args)
        {
            m_animRunning = false;
        }

        protected void BeginAnimation()
        {
            m_animRunning = true;
            m_pageFlipperAnim.Begin();
        }
        #region IScroller Members
        private ScrollContainer m_scrollContainer;
        public ScrollContainer ScrollContainer
        {
            get
            {
                return m_scrollContainer;
            }
            set
            {
                if (m_scrollContainer != value)
                {

                    m_scrollContainer = value;
                }
            }
        }

        #endregion
    }
}